package com.chanyachting.emall.serviceImpl;

import java.util.List;

import com.chanyachting.emall.dao.ProductimageDAO;
import com.chanyachting.emall.domain.Companyproduct;
import com.chanyachting.emall.domain.Productimage;
import com.chanyachting.emall.service.ProductImageService;

public class ProductImageServiceImpl implements ProductImageService {
	
	private ProductimageDAO pidao;

	public ProductImageServiceImpl() {
		
		this.pidao=new ProductimageDAO();
	}

	public boolean deleteAllProductimageByProduct(Companyproduct cp) {
		
		return this.pidao.deleteAllProductimageByProduct(cp);
	}

	public boolean deleteProductimage(Productimage pimg) {
		
		this.pidao.delete(pimg);
		
		return true;
	}

	public boolean existsProductimageUrl(Productimage pimg) {
		
		String args[]={pimg.getImgUrl()};
		
		List list=this.pidao.find("from Productimage where imgUrl=?",args);
		
		if(list!=null && list.size()>0) return true;
		
		return false;
	}

	public List<Productimage> getAllProductimage() {
		
		return this.pidao.find("from Productimage");
	}

	public Productimage getDefaulProductimage(Companyproduct cp) {
		
		String args[]={String.valueOf(cp.getId())};
		
		List list=this.pidao.find("from Productimage where productId=? and isdefaul='Y'",args);
		
		if(list!=null && list.size()>0) return (Productimage)list.get(0);
		
		return null;
	}

	public Productimage getOneProductimage(Productimage pimg) {
		
		return (Productimage) this.pidao.load(Productimage.class, pimg.getId());
	}

	public List<Productimage> getProductimageByProduct(Companyproduct cp) {
		
		String args[]={String.valueOf(cp.getId())};
		
		return this.pidao.find("from Productimage whre productId=?",args);
	}

	public boolean saveProductimage(Productimage pimg) {
		
		this.pidao.save(pimg);
		
		return true;
	}

	public boolean updateProductimage(Productimage pimg) {
		
		this.pidao.update(pimg);
		
		return true;
	}

}
